package resource

import (
	"database/sql"
	"fmt"
	"sync"
)

import (
	_ "github.com/denisenkom/go-mssqldb"
)

import (
	dbc "dbConnector"
	"r_log"
	"util"
)

var ExitStockList map[string]bool
var ExitLock sync.Mutex

var ChooseStockList map[string]bool
var ChooseLock sync.Mutex

type BoardInfo struct {
	MarketCode string
	StartDate  util.DateAndTime
	EndDate    util.DateAndTime
}

func getExitBoardInfoFromDB(db *sql.DB) ([]BoardInfo, error) {
	//rows, err := db.Query("SELECT MarketCode,StartDate,EndDate FROM _HisBoard_Stock WHERE boardid=2243 or boardid = 2254 ")
	rows, err := db.Query("P_RecommenderServer_LoadExitBoardInfo")
	if err != nil {
		return nil, fmt.Errorf("db.Query(): %v", err)
	}
	defer rows.Close()

	var bi []BoardInfo

	marketcode := new(string)
	startdate := new(util.DateAndTime)
	enddate := new(util.DateAndTime)
	for rows.Next() {
		if err := rows.Scan(marketcode, startdate, enddate); err != nil {
			r_log.Logger.Error("getStockPoolsLevelFromDB(): %v", fmt.Errorf("rows.Scan(): %v", err))
			continue
		}
		bi = append(bi, BoardInfo{*marketcode, *startdate, *enddate})
	}
	if err = rows.Err(); err != nil {
		return nil, fmt.Errorf("rows.Err(): %v", err)
	}
	return bi, nil
}

//更新必定剔除股票列表
func (res *Resource) updateExitStockList() error {
	dbc.ServerLock.RLock()
	db := dbc.Servers.BoardInfo.DB
	dbc.ServerLock.RUnlock()

	/*	db, err := s.InitSqlServer()
		if err != nil {
			return fmt.Errorf("initSqlServer(%v): %v", s, err)
		}
		defer db.Close()*/
	ebi, err := getExitBoardInfoFromDB(db)
	if err != nil {
		return fmt.Errorf("getHolidaysFromDB(): %v", err)
	}
	ExitLock.Lock()
	ExitStockList = make(map[string]bool)
	for i := 0; i < len(ebi); i++ {
		if ebi[i].StartDate <= res.NextMarketDay && ebi[i].EndDate >= res.NextMarketDay {
			ExitStockList[ebi[i].MarketCode] = true
			//fmt.Println(ebi[i].MarketCode, len(ebi[i].MarketCode))
		}
	}
	ExitLock.Unlock()
	return nil
}

func getChooseBoardInfoFromDB(db *sql.DB) ([]BoardInfo, error) {
	//rows, err := db.Query("SELECT MarketCode,StartDate,EndDate FROM _HisBoard_Stock WHERE boardid = 2255")
	rows, err := db.Query("P_RecommenderServer_LoadChooseBoardInfo")
	if err != nil {
		return nil, fmt.Errorf("db.Query(): %v", err)
	}
	defer rows.Close()

	var bi []BoardInfo

	marketcode := new(string)
	startdate := new(util.DateAndTime)
	enddate := new(util.DateAndTime)
	for rows.Next() {
		if err := rows.Scan(marketcode, startdate, enddate); err != nil {
			r_log.Logger.Error("getStockPoolsLevelFromDB(): %v", fmt.Errorf("rows.Scan(): %v", err))
			continue
		}
		bi = append(bi, BoardInfo{*marketcode, *startdate, *enddate})
	}
	if err = rows.Err(); err != nil {
		return nil, fmt.Errorf("rows.Err(): %v", err)
	}
	return bi, nil
}

//更新用户可选是否剔除股票列表
func (res *Resource) updateChooseStockList() error {
	dbc.ServerLock.RLock()
	db := dbc.Servers.BoardInfo.DB
	dbc.ServerLock.RUnlock()

	/*	db, err := s.InitSqlServer()
		if err != nil {
			return fmt.Errorf("initSqlServer(%v): %v", s, err)
		}
		defer db.Close()*/
	cbi, err := getChooseBoardInfoFromDB(db)
	if err != nil {
		return fmt.Errorf("getHolidaysFromDB(): %v", err)
	}
	ChooseLock.Lock()
	ChooseStockList = make(map[string]bool)
	for i := 0; i < len(cbi); i++ {
		if cbi[i].StartDate <= res.NextMarketDay && cbi[i].EndDate >= res.NextMarketDay {
			ChooseStockList[cbi[i].MarketCode] = true
			//fmt.Println(ebi[i].MarketCode, len(ebi[i].MarketCode))
		}
	}
	ChooseLock.Unlock()
	return nil
}
